package com.nl.search;


public class BinarySearch {

	private BinarySearch() {
	}

	public static <T extends Comparable<T>> int binarySearch(T[] sortArr, T ele) {

		if (sortArr == null || sortArr.length == 0) {
			throw new RuntimeException();
		}
		
		int left=0,right=sortArr.length-1;
		
		while(left<=right){
			int mid = (left+right)/2;
			if(sortArr[mid].equals(ele)){
				return mid;
			}else if(sortArr[mid].compareTo(ele)>0){
				right=mid-1;
			}else{
				left = mid+1;
			}
		}
		return -1;

	}
	
	public static void main(String[] args) {
		Integer[] arr = new Integer[]{1,22,33,44,55,60,70,100};
		System.out.println(binarySearch(arr, 100));
	}
}
